import django_filters
from .models import Book, Borrowed


# 表格字段筛选
class BooksFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(field_name='name', lookup_expr='contains')
    publisher = django_filters.CharFilter(field_name='publisher', lookup_expr='contains')
    years = django_filters.CharFilter(field_name="years", lookup_expr="contains")
    author = django_filters.CharFilter(field_name="author", lookup_expr="contains")
    genre = django_filters.CharFilter(field_name="genre", lookup_expr="contains")
    country = django_filters.CharFilter(field_name="country", lookup_expr="contains")
    startTime = django_filters.DateTimeFilter(field_name='add_date', lookup_expr='gte')
    endTime = django_filters.DateTimeFilter(field_name='add_date', lookup_expr='lte')

    class Meta:
        model = Book
        fields = ('name', 'publisher', 'startTime', 'endTime', 'years', 'author', "country", "genre")


class BorrowFilter(django_filters.FilterSet):
    borrower = django_filters.CharFilter(field_name="borrower", lookup_expr="contains")  # contains用于模糊匹配
    id_number = django_filters.CharFilter(field_name="id_number", lookup_expr="exact")  # exact用于精准匹配
    startTime = django_filters.DateTimeFilter(field_name='borrow_date', lookup_expr='gte')
    endTime = django_filters.DateTimeFilter(field_name='borrow_date', lookup_expr='lte')

    class Meta:
        model = Borrowed
        fields = ('borrower', 'id_number', 'startTime', 'endTime')
